package com.yunshan.mp;

import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.utils.UserUtils;
import com.yunshan.member.domain.MemberSubCardInfo;
import com.yunshan.member.service.IMemberSubCardInfoService;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.order.domain.OrderUniVo;
import com.yunshan.order.service.IOrderConsumeRecordService;
import com.yunshan.order.service.IOrderRechargeRecordService;
import com.yunshan.wechat.domain.WechatUserInfo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static com.yunshan.framework.datasource.DynamicDataSourceContextHolder.log;


/**
 * 余额明细Controller
 *
 * @author yunshan
 * @date 2025-02-18
 */
@Api(tags = "余额明细")
@RestController
@RequestMapping("/wx/balance")
public class BalanceController extends BaseController
{

    @Autowired
    private IOrderConsumeRecordService orderConsumeRecordService;
    @Autowired
    private IOrderRechargeRecordService orderRechargeRecordService;
    @Autowired
    private IMemberUserInfoService memberUserInfoService;
    @Autowired
    private IMemberSubCardInfoService memberSubCardInfoService;
    @Autowired
    private IWechatUserInfoService wechatUserInfoService;

    @ApiOperation("余额明细查询")
    @GetMapping()
    private AjaxResult getBalanceInfo(
            @ApiParam(name = "type", value = "1:消费 2:充值 ", required = true)
            @RequestParam(required = false) Integer type)
    {
        try {
            List<OrderUniVo> orderUniVos = new ArrayList<>();
            Long memberId;
            WechatUserInfo wechatUserInfo = wechatUserInfoService.selectWechatUserInfoById(UserUtils.getUserId());

            //主卡用户
            if (wechatUserInfo.getType() == 1) {
                memberId = memberUserInfoService.selectUserInfoByUserId(wechatUserInfo.getId()).getId();

                if (type == null || type != 2) {
                    //主卡消费订单
                    List<OrderUniVo> memberConsume = orderConsumeRecordService.selectOrderUniList(memberId,1);
                    if (memberConsume.size() > 0) {
                        orderUniVos.addAll(memberConsume);
                    }

                    //副卡消费订单
                    MemberSubCardInfo memberSubCardInfo = new MemberSubCardInfo();
                    memberSubCardInfo.setMemberId(memberId);
                    List<MemberSubCardInfo> memberSubCardInfos = memberSubCardInfoService.selectMemberSubCardInfoList(memberSubCardInfo);
                    if (memberSubCardInfos != null) {
                        for (MemberSubCardInfo subCardInfo : memberSubCardInfos) {
                            List<OrderUniVo> subOrderUniVo = orderConsumeRecordService.selectOrderUniList(subCardInfo.getId(),2);
                            //拼接副卡名字
                            if (subOrderUniVo.size() > 0) {
                                for (OrderUniVo orderUniVo : subOrderUniVo) {
                                    orderUniVo.setMemberName(orderUniVo.getMemberName() + "（副卡名称）");
                                }
                                orderUniVos.addAll(subOrderUniVo);
                            }
                        }
                    }
                }

                if (type == null || type != 1) {
                    //充值订单
                    List<OrderUniVo> orderRecharge = orderRechargeRecordService.selectOrderUniList(memberId);
                    if (orderRecharge.size() > 0) {
                        orderUniVos.addAll(orderRecharge);
                    }
                }

            } else {
                if (type == null || type != 2) {
                    //副卡用户只返回消费订单
                    memberId = memberSubCardInfoService.selectUserInfoByUserId(wechatUserInfo.getId()).getId();
                    List<OrderUniVo> subConsume = orderConsumeRecordService.selectOrderUniList(memberId,2);
                    if (subConsume.size() > 0) {
                        orderUniVos.addAll(subConsume);
                    }
                }
            }

            // 时间倒序
            if (orderUniVos.size() > 0){
                orderUniVos.sort(Comparator
                        .comparing(OrderUniVo::getOrderTime, Comparator.nullsLast(Comparator.reverseOrder()))
                        .thenComparing(OrderUniVo::getServeName)
                );
            }

            return success(orderUniVos);
        }catch (Exception e){
            log.error("余额明细查询：{}",e.getMessage());
            return AjaxResult.error();
        }
    }

}
